package com.bocai.yuweiwangluo.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.View.RoundImageView;
import com.bocai.yuweiwangluo.activity.CouponsActivity;
import com.bocai.yuweiwangluo.activity.MessageCenterActivity;
import com.bocai.yuweiwangluo.activity.MyGameActivity;
import com.bocai.yuweiwangluo.activity.MyInvitationActivity;
import com.bocai.yuweiwangluo.activity.MyMatchActivity;
import com.bocai.yuweiwangluo.activity.MyVenueActivity;
import com.bocai.yuweiwangluo.activity.PersonalInformationActivity;
import com.bocai.yuweiwangluo.activity.SettingActivity;
import com.bocai.yuweiwangluo.base.BaseFragment;
import com.bocai.yuweiwangluo.bean.TypesBean;
import com.bocai.yuweiwangluo.bean.UserInfoBean;
import com.bocai.yuweiwangluo.net.MyOkHttpClient;
import com.bocai.yuweiwangluo.utils.ImageLoaderUtil;
import com.bocai.yuweiwangluo.utils.MyConst;
import com.bocai.yuweiwangluo.utils.SharePrefencesUtil;
import com.bocai.yuweiwangluo.utils.UrlData;
import com.google.gson.Gson;

import butterknife.ButterKnife;


/**
 * Created by admin on 2015/9/16.
 */
public class Fragment_Me extends BaseFragment implements View.OnClickListener
{
    private View view;
    private LinearLayout ll_geren,ll_youhui,ll_dingchang,ll_yuezhan,ll_game,ll_yaoqing,ll_title_left_back,ll_title_right;
    private TextView title_mame,tv_couponcount,tvNickname;
    private ImageView iv_title_right,iv_title_left;
    private static final int RESPONSE_SUCCEED = 1;
    private static final int RESPONSE_FAILED = 2;
    private TypesBean typesBean=new TypesBean();
    private UserInfoBean bean=new UserInfoBean();
    private int requstType;
    private RoundImageView photo;
    private Handler mHandler=new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
            String result = (String) msg.obj;
            Gson gson = new Gson();
            try {
                switch (msg.what)
                {
                    case RESPONSE_SUCCEED:
                        Log.e("loginresult", result);
                        if(requstType==1)
                        {
                            typesBean = gson.fromJson(result, TypesBean.class);
                            MyConst.type = typesBean.getType();
                            getcount();
                        }else
                        {
                            hideLoading();
                            bean=gson.fromJson(result,UserInfoBean.class);
                            tv_couponcount.setText(bean.getRow().getCoupon_num());
                            tvNickname.setText(bean.getRow().getNickname());
                            ImageLoaderUtil.loaderImage(UrlData.IMAGEURL + bean.getRow().getIcon(), photo, R.mipmap.me);
                        }
                        break;
                    case RESPONSE_FAILED:
                        hideLoading();
                        showError(result);
                        break;
                }
            } catch (Exception e)
            {
            }
        }
    };

    private void getcount()
    {
        requstType=2;
        MyOkHttpClient myOkHttpClient=new MyOkHttpClient(getActivity(),this);
        myOkHttpClient.getInfo(UrlData.BASEURL + UrlData.GETINFO, SharePrefencesUtil.getUser_id(getActivity()), mHandler);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        view = inflater.inflate(R.layout.fragment_me, container, false);
        initView();
        ButterKnife.bind(this, view);
        initEvent();
        getTypes();
        return view;
    }

    private void getTypes()
    {
        showLoading();
        requstType=1;
        MyOkHttpClient myOkHttpClient=new MyOkHttpClient(getActivity(),this);
        myOkHttpClient.getbbsType(UrlData.BASEURL+UrlData.BBSGETTYPE,mHandler);
    }

    private void initEvent()
    {
        ll_geren.setOnClickListener(this);
        ll_youhui.setOnClickListener(this);
        ll_dingchang.setOnClickListener(this);
        ll_yuezhan.setOnClickListener(this);
        ll_game.setOnClickListener(this);
        ll_yaoqing.setOnClickListener(this);
        ll_title_left_back.setOnClickListener(this);
        ll_title_right.setOnClickListener(this);
    }

    private void initView()
    {
        tvNickname=(TextView)view.findViewById(R.id.tv_nickname);
        photo=(RoundImageView)view.findViewById(R.id.photo);
        tv_couponcount=(TextView)view.findViewById(R.id.tv_couponcount);
        title_mame=(TextView)view.findViewById(R.id.title_mame);
        title_mame.setText("我的");
        iv_title_right=(ImageView)view.findViewById(R.id.iv_title_right);
        iv_title_right.setImageResource(R.mipmap.me_message);
        iv_title_left=(ImageView)view.findViewById(R.id.iv_title_left);
        iv_title_left.setImageResource(R.mipmap.me_setting);
        ll_title_left_back=(LinearLayout)view.findViewById(R.id.ll_title_left_back);
        ll_title_right=(LinearLayout)view.findViewById(R.id.ll_title_right);
        ll_geren=(LinearLayout)view.findViewById(R.id.ll_geren);
        ll_youhui=(LinearLayout)view.findViewById(R.id.ll_youhui);
        ll_dingchang=(LinearLayout)view.findViewById(R.id.ll_dingchang);
        ll_yuezhan=(LinearLayout)view.findViewById(R.id.ll_yuezhan);
        ll_game=(LinearLayout)view.findViewById(R.id.ll_game);
        ll_yaoqing=(LinearLayout)view.findViewById(R.id.ll_yaoqing);


    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if(requestCode==1000)
        {
            if(resultCode==-1)
            {
                if(data!=null)
                {
                    String photopath=data.getStringExtra("photo");
                    String nickname=data.getStringExtra("nickname");
                    if(!("".equals(photopath)))
                    {
                        ImageLoaderUtil.loaderImage(UrlData.IMAGEURL+photopath,photo,R.mipmap.me);
                    }
                    if(!("".equals(nickname)))
                    {
                        tvNickname.setText(nickname);
                    }
                }
            }
        }
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.ll_geren:
                Intent intent=new Intent(getActivity(), PersonalInformationActivity.class);
                intent.putExtra("types",typesBean);
                startActivityForResult(intent, 1000);
                break;
            case R.id.ll_youhui:
                startActivity(new Intent(getActivity(), CouponsActivity.class));
                break;
            case R.id.ll_dingchang:
                startActivity(new Intent(getActivity(), MyVenueActivity.class));
                break;
            case R.id.ll_yuezhan:
                startActivity(new Intent(getActivity(), MyMatchActivity.class));
                break;
            case R.id.ll_game:
                startActivity(new Intent(getActivity(), MyGameActivity.class));
                break;
            case R.id.ll_yaoqing:
                startActivity(new Intent(getActivity(), MyInvitationActivity.class));
                break;
            case R.id.ll_title_left_back:
                startActivity(new Intent(getActivity(), SettingActivity.class));
                break;
            case R.id.ll_title_right:
                startActivity(new Intent(getActivity(), MessageCenterActivity.class));
                break;
        }
    }

    @Override
    public void onDestroyView()
    {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
